<?php

class OrderablesController extends AppController {

	var $helpers = array("Html","Text","Javascript","Ajax","Image", 'Lightbox');
	
	function setup() {
		$this->set('message', $this->Orderable->initialize());
	}
	
	function order($id) {
		if	(!$this->checkSession()) {
			$this->Session->setFlash("You must log in to order products.");
			$this->Session->write('goto','/orderables/order/' . $id);
			$this->redirect("/members/login");
			exit;
		}
		if (empty($id)) {
			$this->Session->setFlash("Huh?");
			$this->redirect("/");
			exit;
		}
		$forceOrder = ($this->userInfo['auth_type'] == AUTH_CASH || $this->userInfo['auth_type'] == AUTH_ADMIN);
		$o = $this->Orderable->c_find('Orderable.id = ' . $id);
		$o = $o[0];
		if (!empty($this->data)) { //attempt to save
			$b = $this->Orderable->BasketItem->find("BasketItem.member_id = '" . $this->data['BasketItem']['member_id'] . "' AND BasketItem.orderable_id = '" . $id . "'");
			if (!empty($b)) {
				$this->data['BasketItem']['id'] = $b['BasketItem']['id'];
				$this->data['BasketItem']['quantity'] += $b['BasketItem']['quantity'];
			}
			if ($this->Orderable->BasketItem->save($this->data)) {
				$f = '';
				if (empty($b)) {
					$this->data['BasketItem']['id'] = $this->Orderable->BasketItem->getLastInsertId();
				}
				if ($o['Orderable']['inventory_start'] > 0) {
					$this->Orderable->id = $id;
					if ($forceOrder) {
						// don't worry about the inventory.
					} else {
						$q = (!empty($b)) ? $b['BasketItem']['quantity'] : $this->data['BasketItem']['quantity'];
						if ($q > $o['Orderable']['inventory_left']) {
							$this->Orderable->saveField('inventory_left', 0);
 							$this->data['BasketItem']['quantity'] = $o['Orderable']['inventory_left'];
 							$this->Orderable->BasketItem->save($this->data);
 							$f = '	Only ' . $o['Orderable']['inventory_left'] . ' of that item were available.	You got the last ones!';
						} else {
							$this->Orderable->saveField('inventory_left',$o['Orderable']['inventory_left'] - $q);
						}
					}
				}
				$redir = '/members/menu';
				if ($this->Session->valid()){
					if ($this->Session->check('go_back')) {
						$g = $this->Session->read('go_back');
						if (in_array('css',$g)) {
							$this->Session->del('go_back');
						} else {
							$redir = join('/',$this->Session->read('go_back'));
						}
						$this->Session->del('go_back');
					}
				}
				$this->Session->setFlash("Item(s) added to basket" . $f,'message_ok');
				$this->redirect($redir);
			} else {
				//data not valid, throw errors
			}
		}
		if($this->checkLevel(AUTH_CASH) && $this->Session->check('current_basket')) {
			$this->set('toBasket', $this->Session->read('current_basket'));
		}
		$this->set('order',$o);
		App::import('Model', 'Picture');
		$pics = new Picture();
		$u = $pics->getUserIdFromEmail($o['Member']['email_address']);
		$this->set('flickr_user', $u);
		$pic_error = $pics->getError();
		$pictures = array();
		if ($pic_error === false) {
			$pictures = $pics->findAll(array('user_id' => $u['nsid'], 'tags' => "" . $o['Product']['product_id']));
			$pic_error = $pics->getError();
		}
		$photos = array();
		if (!empty($pictures['photo'])) {
			foreach($pictures['photo'] as $photo) {
				$photos[] = array('thumb' => $pics->getUrl($photo, 'thumbnail'), 'medium' => $pics->getUrl($photo), 'title' => $photo['title']);
			}
		}
		$this->set('pictures', $pictures);
		$this->set('photos', $photos);
		$this->set('pic_error', $pic_error);
		$this->render();
	}
	
}
?>
